package com.yanqu.road.server.cmd.activity.suitang.cross;

import com.yanqu.road.entity.activity.suitang.config.SuiTangConfigSync;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.suitang.SuiTang;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.suitang.SuiTangMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.text.MessageFormat;

@Cmd(code = GameProtocol.S_SUI_TANG_AUTO_SETTING_INGOTS_OPERATE_FROM_CROSS, desc = "自动设置元宝操作跨服回包")
public class SuiTangAutoSettingIngotsOperateFromCrossCmd extends AbstractCommand {

    private SuiTang.CrossSuiTangAutoSettingIngotOperateFromCrossMsg reqMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = SuiTang.CrossSuiTangAutoSettingIngotOperateFromCrossMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        SuiTangConfigSync config = SuiTangMgr.getConfig();
        if (config == null) {
            return;
        }

        SuiTang.SuiTangAutoSettingIngotsOperateRespMsg.Builder respMsg = SuiTang.SuiTangAutoSettingIngotsOperateRespMsg.newBuilder();

        Property ingotsCost = null;
        if (reqMsg.getOperateType() == 1) {
            // 预扣除返还
            ingotsCost = new Property(GameConfig.GAME_MONEY_INGOTS, BigInteger.valueOf(reqMsg.getOffsetIngotsTimes()).multiply(BigInteger.valueOf(config.getParamConfig().getAutoSettingIngotsNum())));
            if (player != null) {
                player.getModule(PreReduceModule.class).restorePreReduce(ingotsCost);
            } else {
                log.error("自动设置元宝操作跨服回包 restorePreReduce error, player is null,userId:{},offsetIngotsTimes:{}", reqMsg.getUserId(), reqMsg.getOffsetIngotsTimes());
            }
        }
        if (reqMsg.getRet() == 0) {
            // 成功了
            if (reqMsg.getOperateType() == 1) {
                // 增加次数
                if (player != null) {
                    player.getModule(CurrencyModule.class).removeCurrency(ingotsCost, eLogMoneyType.SuiTang, eLogMoneyType.SuiTangAutoSettingIngotsOperate);
                } else {
                    log.error("自动设置元宝操作跨服回包 removeCurrency error, player is null,userId:{},offsetIngotsTimes:{}", reqMsg.getUserId(), reqMsg.getOffsetIngotsTimes());
                }
            } else if (reqMsg.getOperateType() == 2 && reqMsg.getOffsetIngotsTimes() > 0) {
                // 取回
                Property ingotsGet = new Property(GameConfig.GAME_MONEY_INGOTS, BigInteger.valueOf(reqMsg.getOffsetIngotsTimes()).multiply(BigInteger.valueOf(config.getParamConfig().getAutoSettingIngotsNum())));

                UserInfo userInfo = UserMgr.getUserInfo(reqMsg.getUserId());
                if (userInfo != null) {
                    String activityName = ActivityMgr.getActivityName(config.getActivityInfo(), userInfo.getLanguage());
                    String mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.SUITANG_MAIL_TITLE_CITY_MOVE_ITEM_RETURN, userInfo.getLanguage()), activityName);
                    String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.SUITANG_MAIL_CONTENT_CITY_MOVE_ITEM_RETURN, userInfo.getLanguage()), activityName);
                    MailManager.sendMail(reqMsg.getUserId(), eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(ingotsGet), mailContent, mailTitle, config.getActivityInfo().getActivityId(), config.getActivityInfo().getServerListStr());
                }
            }
            respMsg.setRet(0);
            respMsg.setIngotsTimes(reqMsg.getIngotsTimes());
        } else {
            respMsg.setRet(reqMsg.getRet());
        }
        // 返回
        if (player != null) {
            player.sendPacket(ClientProtocol.U_SUI_TANG_AUTO_SETTING_INGOTS_OPERATE, respMsg);
        }
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
